package com.srmt.document.feishu.internal.util;

import com.lark.oapi.service.docx.v1.model.Block;
import com.srmt.document.exception.TableException;
import com.srmt.document.feishu.internal.core.BlockContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Block上下文辅助工具类
 * 统一处理Block查找、验证和表格定位的通用模式
 * 
 * @author srmt
 * @since 1.1.0
 */
public class BlockContextHelper {
    
    private static final Logger logger = LoggerFactory.getLogger(BlockContextHelper.class);
    
    /**
     * 查找并验证锚点后的表格
     * 
     * @param context Block上下文
     * @param anchorText 锚点文本
     * @return 找到的表格Block
     * @throws TableException 如果表格未找到
     */
    public static Block findAndValidateTable(BlockContext context, String anchorText) 
            throws TableException {
        
        if (context == null) {
            throw new IllegalArgumentException("块Context 不能为null");
        }
        
        if (anchorText == null || anchorText.trim().isEmpty()) {
            throw new IllegalArgumentException("Anchor text 不能为null or empty");
        }
        
        logger.debug("查找 table after anchor: {}", anchorText);
        
        Block tableBlock = context.findTableAfterAnchor(anchorText);
        
        if (tableBlock == null) {
            String errorMsg = String.format("Table not found after anchor: %s", anchorText);
            logger.error(errorMsg);
            throw new TableException(errorMsg);
        }
        
        logger.debug("表格 found successfully after anchor: {}", anchorText);
        return tableBlock;
    }
    
    /**
     * 查找并验证包含锚点的表格
     * 
     * @param context Block上下文
     * @param anchorText 锚点文本（表格内的文本）
     * @return 找到的表格Block
     * @throws TableException 如果表格未找到
     */
    public static Block findAndValidateTableContaining(BlockContext context, String anchorText) 
            throws TableException {
        
        if (context == null) {
            throw new IllegalArgumentException("块Context 不能为null");
        }
        
        if (anchorText == null || anchorText.trim().isEmpty()) {
            throw new IllegalArgumentException("Anchor text 不能为null or empty");
        }
        
        logger.debug("查找包含文本的表格: {}", anchorText);
        
        // 使用现有的方法查找包含锚点的表格
        Block tableBlock = context.findTableAfterAnchor(anchorText);
        
        if (tableBlock == null) {
            String errorMsg = String.format("Table containing text '%s' not found", anchorText);
            logger.error(errorMsg);
            throw new TableException(errorMsg);
        }
        
        logger.debug("表格 found successfully containing text: {}", anchorText);
        return tableBlock;
    }

    /**
     * 查找文本元素后的第一个Block
     *
     * @param context Block上下文
     * @param anchorText 锚点文本
     * @return 找到的Block，如果未找到返回null
     */
    public static Block findBlockAfterText(BlockContext context, String anchorText) {

        if (context == null || anchorText == null) {
            return null;
        }

        logger.debug("查找 block after text: {}", anchorText);

        // 使用现有的findBlockByAnchor方法
        return context.findBlockByAnchor(anchorText);
    }
}